import {PolicyDetailProvider} from './../../../providers/policy/policy-detail/policy-detail.provider';

import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {UniversalAccount} from "../../../model/pages/policy/policy-detail/universal-account";
import {HttpErrorResponse} from "@angular/common/http";
import {LoadingService} from "../../../services/common/loading.service";

/**
 * Generated class for the UniversalAccountDetailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-universal-account-detail',
    templateUrl: 'universal-account-detail.html',
    providers: [PolicyDetailProvider, LoadingService]
})
export class UniversalAccountDetailPage {
    /**
     * 万能险详情数据
     * @type {UniversalAccount}
     */
    universalAccountGroup: UniversalAccount = new UniversalAccount();

    displayInd: string;

    basePlanCode: string;

    /**
     * 可追加金额
     * @type {string}
     */
    rulUniversallimitAmount = '';

    /**
     * 可追加金额计算显示结果
     * @type {string}
     */
    computedAmountDisplay: string = '';

    /**
     * 是否加载完数据
     * @type {boolean}
     */
    isLoaded: boolean = false;

    /**
     * 获取数据订阅
     */
    getDataSubscription: any;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        private policyDetailProvider: PolicyDetailProvider,
        private toastController: ToastController,
        private loadingService: LoadingService) {
    }

    ionViewDidLoad() {

        // 获取id
        let id = this.navParams.get('id');
        // 获取可追加金额
        this.rulUniversallimitAmount = this.navParams.get('rulUniversallimitAmount');
        this.displayInd = this.navParams.get('displayInd');
        this.basePlanCode = this.navParams.get('basePlanCode');

        // console.log('displayInd:'+ this.displayInd);
        // console.log('basePlanCode' + this.basePlanCode);

        if(this.displayInd === 'N') {
            if (this.basePlanCode.indexOf('VUA')>-1) {
                this.computedAmountDisplay = '无限额';
            } else {
                this.computedAmountDisplay = '￥'+this.rulUniversallimitAmount;
            }
        }

        if (id != null) {

            // 显示loading动画
            this.loadingService.showLoading();

            // 请求数据
            this.policyDetailProvider.getPlicyUniversallList(id).subscribe(data => {
                console.log(data);
                if (data) {
                    this.universalAccountGroup = data[0]
                }

                // 加载数据结束
                this.isLoaded = true;

                // 关闭loading动画
                this.loadingService.dismissLoading();
            }, (error: HttpErrorResponse) => {
                // 加载数据结束
                this.isLoaded = true;
                // 关闭loading动画
                this.loadingService.dismissLoading();
            })
        }
    }

    ionViewWillLeave() {
        // 取消订阅数据
        if (this.getDataSubscription) {
            this.getDataSubscription.unsubscribe();
        }
    }

    /**
     * 返回上一级
     */
    goBack() {
        this.navCtrl.pop();
    }

}
